package com.thread.consumer;

public class Desk {
    private boolean conduct;
    public synchronized void setConduct(){
        try{
            if(conduct){
                this.notify();
                this.wait();
            }else{
                System.out.println(Thread.currentThread().getName() + "放上了一个面包");
                Thread.sleep(1000);
                conduct = true;
                this.notify();
                this.wait();
            }
        }
        catch(Exception e){
            System.out.println(e.getMessage());
        }
    }
    public synchronized void getConduct(){
        try{
            if(conduct){
                System.out.println(Thread.currentThread().getName() + "吃了一个面包");
                Thread.sleep(1000);
                conduct = false;
                this.notify();
                this.wait();
            }else{
                this.notify();
                this.wait();
            }
        }
        catch(Exception e){
            System.out.println(e.getMessage());
        }
    }
    public static void main(String[] args){
        Desk desk = new Desk();
        Conductor con1 = new Conductor(desk);
        Conductor con2 = new Conductor(desk);
        Conductor con3 = new Conductor(desk);
        Thread t1 = new Thread(con1, "厨师1");
        Thread t2 = new Thread(con2, "厨师2");
        Thread t3 = new Thread(con3, "厨师3");
        Consumer consumer1 = new Consumer(desk);
        Consumer consumer2 = new Consumer(desk);
        Thread t4 = new Thread(consumer1, "顾客1");
        Thread t5 = new Thread(consumer2, "顾客2");
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();
    }

}
